package offer;

import java.util.HashSet;
import java.util.Set;

public class offer_0052 {
    ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Set<ListNode> set=new HashSet<>();
        ListNode p=headA;
        ListNode q=headB;
        while(headA!=null){
            set.add(headA);
            headA=headA.next;
        }
        while(headB!=null){
            if(set.contains(headB)) return headB;
            headB=headB.next;
        }
        headA=p;
        headB=q;
        return null;
    }
}
